package com.mobileservice.autoreply.data.database;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.mobileservice.autoreply.ui.model.AutoSMSModel;
import com.mobileservice.autoreply.ui.model.PhoneContactModel;

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

	public static final String DATABASE_NAME = "anle.db";
	public static final int DATABASE_VERSION = 1;

	/** Dao Object we use to access PhoneContactModel Database */
	private Dao<PhoneContactModel, Long> phoneContactDao;
	private RuntimeExceptionDao<PhoneContactModel, Long> phoneContactExDao;

	/** Dao Object we use to access AutoSMSModel database */
	private Dao<AutoSMSModel, Long> AutoSMSDao;
	private RuntimeExceptionDao<AutoSMSModel, Long> AutoSMSExDao;

	public DatabaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase arg0, ConnectionSource arg1) {
		try {
			Log.i(DatabaseHelper.class.getName(), "onCreate");
			TableUtils.createTable(connectionSource, PhoneContactModel.class);
			TableUtils.createTable(connectionSource, AutoSMSModel.class);
		} catch (SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Can't create database", e);
			throw new RuntimeException(e);
		}

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, ConnectionSource arg1, int arg2, int arg3) {
		try {
			Log.i(DatabaseHelper.class.getName(), "onUpgrade");
			TableUtils.dropTable(arg1, PhoneContactModel.class, true);
			TableUtils.dropTable(arg1, AutoSMSModel.class, true);
			// after we drop the old databases, we create the new ones
			onCreate(db, connectionSource);
		} catch (SQLException e) {
			Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
			throw new RuntimeException(e);
		}
	}

	/**
	 * Returns the Database Access Object (DAO) for our Table class. It will
	 * create it or just give the cached value.
	 */
	public Dao<PhoneContactModel, Long> getPhoneContactDao() throws SQLException {
		if (phoneContactDao == null) {
			phoneContactDao = getDao(PhoneContactModel.class);
		}
		return phoneContactDao;
	}

	public Dao<AutoSMSModel, Long> getAutoSmsDao() throws SQLException {
		if (AutoSMSDao == null) {
			AutoSMSDao = getDao(AutoSMSModel.class);
		}
		return AutoSMSDao;
	}

	/**
	 * Returns the RuntimeExceptionDao (Database Access Object) version of a Dao
	 * for our Table class. It will create it or just give the cached value.
	 * RuntimeExceptionDao only through RuntimeExceptions.
	 */
	public RuntimeExceptionDao<PhoneContactModel, Long> getPhoneContactExDao() {
		if (phoneContactExDao == null) {
			phoneContactExDao = getRuntimeExceptionDao(PhoneContactModel.class);
		}
		return phoneContactExDao;
	}

	public RuntimeExceptionDao<AutoSMSModel, Long> getAutoSmsExDao() throws SQLException {
		if (AutoSMSExDao == null) {
			AutoSMSExDao = getRuntimeExceptionDao(AutoSMSModel.class);
		}
		return AutoSMSExDao;
	}

}
